﻿<%@ WebHandler Language="VB" Class="Upload" %>
Imports System
Imports System.Web
Imports System.Data
Imports VSModel
Imports Newtonsoft.Json
Imports System.Xml
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports Cls_Common
Public Class Upload : Implements IHttpHandler, IRequiresSessionState
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
       
        Dim file As HttpPostedFile = CType(context.Request.Files("myfile"), HttpPostedFile)
        
        Dim rootPath As String = context.Server.MapPath("~/Uploads/")
        
        Dim intFileNameLength As Integer
        Dim strFileName As String
        Dim random As New Random
        'Logic to find the FileName (excluding the path)
        Dim DestPath = rootPath & "\" & Now.Year.ToString.Trim() & "\" & Now.Month.ToString.Trim() & "-" & Now.Day.ToString.Trim()
        Dim StringDate = Now.Year.ToString.Trim() & "/" & Now.Month.ToString.Trim() & "-" & Now.Day.ToString.Trim()
        'create thu mutc theo year + month
        If Not Directory.Exists(DestPath) Then
            Directory.CreateDirectory(DestPath)
        End If
        strFileName = file.FileName
        'neu ton tai file name 
        If (System.IO.File.Exists(DestPath & "/" & strFileName)) Then
            intFileNameLength = InStr(1, StrReverse(strFileName), "\")
            strFileName = (random.NextDouble + 65).ToString + strFileName
            context.Request.Files("myfile").SaveAs(DestPath & "/" & strFileName)
        Else
            context.Request.Files("myfile").SaveAs(DestPath & "/" & strFileName)
        End If

        'check if name contain image type
        Dim ImageType As String() = {"JPG", "GIF", "PNG", "JPEG", "BMP"}
        Dim ext As String = strFileName.Substring(strFileName.LastIndexOf(".") + 1, strFileName.Length - strFileName.LastIndexOf(".") - 1)
        Dim check As Boolean = IIf(ImageType.Contains(ext.ToUpper()), True, False)
        ' Check isImage
        If check Then
            Dim imgWidth As Integer
            Dim imgHeight As Integer
            Dim strNewFile As String
            Dim path As String = ""
            Dim thumbPath As String
            Dim arrImageHot(,) As Integer = {{120, 120}, {212, 180}, {160, 180}, {210, 280}}
            For i = 0 To UBound(arrImageHot)
          
                imgWidth = arrImageHot(i, 0)
                imgHeight = arrImageHot(i, 1)
           
                path = DestPath & "/" & strFileName
            
                strNewFile = imgWidth.ToString & "-" & imgHeight & "-" & strFileName
                thumbPath = DestPath & "/" & strNewFile
            
                Dim sourceFile = System.IO.File.OpenRead(path)
                Dim desFile = System.IO.File.Create(thumbPath)
                ResizeImage(imgWidth, imgHeight, sourceFile, desFile)
            
                sourceFile.Close()
                desFile.Close()
        
            Next
        End If
        context.Response.Write(StringDate & "/" & strFileName)
    End Sub
 
    Protected Sub ResizeImage(ByVal width As Integer, ByVal height As Integer, ByVal fromStream As Stream, ByVal toStream As Stream)

        Dim image As Image = image.FromStream(fromStream)
        Dim newWidth = width
        Dim newHeight = CInt(image.Height * (width / image.Width))
        Dim thumbnailBitmap = New Bitmap(newWidth, newHeight)
 
        Dim thumbnailGraph = Graphics.FromImage(thumbnailBitmap)
        thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality
        thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality
        thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic
 
        Dim imageRectangle = New Rectangle(0, 0, newWidth, newHeight)
        thumbnailGraph.DrawImage(image, imageRectangle)
        Dim imageCrop = New Rectangle(0, 0, newWidth, newHeight)
        'crop image here
        'image.RawFormat
        
        thumbnailBitmap.Save(toStream, image.RawFormat)
        thumbnailGraph.Dispose()
        thumbnailBitmap.Dispose()
        image.Dispose()
        
    End Sub
        

    
    
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class